// pages/my/my.js
var md=require('../../utils/md5.js');
var Trim = require('../../utils/util.js');
var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
var app;
var url;
var headers;
Page({
  data:{
    expert:"",
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",
    islogin:"",
    phone_num:"",
    psw:"",
    infor:{},
    status:{},
    myTrval:{}
    // tabBar: {
    //   index: {
    //     img: "index_.png"
    //   },
    //   classfiy: {
    //     img: "sort_.png"
    //   },
    //   trval: {
    //     img: "expert_icon.png"
    //   },
    //   goal: {
    //     img: "adr_.png"
    //   },
    //   my: {
    //     img: "my.png"
    //   }
    // }
  },
  // toindex: function () {
  //   wx.redirectTo({
  //     url: '../index/index',
  //   })
  // },
  // tosort: function () {
  //   wx.redirectTo({
  //     url: '../sort/sort',
  //   })
  // },
  // togoal: function () {
  //   wx.redirectTo({
  //     url: '../destination/destination',
  //   })
  // },
  // tomy: function () {
  //   wx.redirectTo({
  //     url: '../my/my',
  //   })
  // },
  regester:function(){ 
    wx.navigateTo({
      url: '../../pages/register/register'
    })
  },
  getinfo:function (e){
    if (e.target.dataset.type == "phone_num") {//电话号
      this.setData({
        phone_num: Trim.trim(e.detail.value)
      });
    } else if (e.target.dataset.type == "password"){
      this.setData({
        psw: Trim.trim(e.detail.value)
      });
    };
  },

  bindGetUserInfo:function(e){
        console.log(e);
  },
  wxlogin:function(){
    var that=this;
    wx.login({
      success: function (res) {
        console.log(res);
        var wxMes = app.globalData.wxMes;
        if(res.code){
          var code = res.code;
          wx.request({
            url: 'httpss://api.weixin.qq.com/sns/jscode2session?appid=' 
            + wxMes.appId + '&secret=' 
            + wxMes.secret+'&js_code='
            +code+'&grant_type=authorization_code',
            method:"POST",
            data:{
              appid: wxMes.appId,
              secret: wxMes.secret,
              js_code: code,
              grant_type:'authorization_code'
            },
            success:function(data){
              console.log(data)
            }
          })
        }
        wx.getUserInfo({//获取昵称
          success:function(res){
            let screen = app.globalData.headers.Screen;
            let os = app.globalData.headers.Os;
            console.log(res);
            wx.setStorageSync('infor', JSON.stringify(res.userInfo));
            app.globalData.headers = {
              "Content-Type": "application/x-www-form-urlencoded",
              "Device-Id": "",
              "Os": os,
              "Screen": screen,
              "User-Token":res.signature
            };
            app.globalData.islogin=true;
            that.getMyTrval(); 
          },
        });

      }
      
    });
  },
  //登录 以及登录处理
  login:function(e){
   app.globalData.islogin = true;
   this.setData({
     islogin:app.globalData.islogin
   })
  var that=this    
    let login_type;
    let phone_num=null; 
    var password;
    var psw =this.data.psw;
    console.log(psw)
    if(e.target.dataset.type=="phone"){//手机登录
      login_type=1;
      phone_num=this.data.phone_num;
      password = md.md5(md.md5(psw));
      if (!phone_num){
        wx.showModal({
          title: '来自伴行的提示',
          content: '请输入手机号',
          showCancel:false
        })
        return
      } else if (!psw){
        wx.showModal({
          title: '来自伴行的提示',
          content: '密码不能为空',
          showCancel:false
        })
        return;
      }
    }
    let url = app.globalData.url;
    let headers= app.globalData.headers;
    // if(e.target.detail)
    wx.request({
      url: url +'api/login',
      method:"POST",
      header: headers,
      data:{
        "login_type":login_type,
        "phone_num":phone_num,
        "password":password
      },
      success:function(data){
        let screen = app.globalData.headers.Screen;
        let os = app.globalData.headers.Os;
        
       if(data.data.result==1){
         console.log(data.data.data.token)        
         wx.showToast({
           title:"登录成功",
           icon:"success",
           duration: 2000
         })
         wx.showModal({
           title:"登录信息",
           content:"登录成功",
           showCancel:false,
           complete:function(res){
             if (res.confirm){
               app.globalData.headers = {
                 "Content-Type": "application/x-www-form-urlencoded",
                 "Device-Id": "",
                 "Os": os,
                 "Screen": screen,
                 "User-Token": data.data.data.token
               };
               try {
                 wx.setStorageSync('infor', JSON.stringify(data.data.data));
                 
               } catch (e) {
               };
               app.globalData.islogin = true;
               that.getMyTrval();                    
             }
           } 
         })
       }else{
         wx.showModal({
           title: "登录信息",
           content:data.data.message,
           showCancel: false,
           complete: function (res) {
             if (res.confirm) {
                
             }
           }
         })
       }
      }
    })
  },
  getMyTrval:function(){//本地缓存我的旅游专家
    var app=getApp();
    
    wx.request({//我的旅游专家信息
      url:url+'api/get_my_travel_mavin',
      header: app.globalData.headers,
      success:function(data){
        console.log(app.globalData.headers)
        console.log(data)
        if(data.data.result===1){
          try{
            wx.setStorageSync("myTrval",JSON.stringify(data.data.data));
            // wx.switchTab({
            //   url: '../../pages/index/index',
            // })
            var u=wx.getStorageSync("loginUse");
            var v = wx.getStorageSync("loginUseC");
           if(u){
             wx.navigateTo({
               url: u,
             });
           } else if (v){
             wx.navigateTo({
               url:v,
             });
           }else{
             wx.switchTab({
               url: '../index/index',
             });
           }
          }catch(e){

          }
        }
      }
    })
    wx.request({//系统全局配置
      url: url +'api/query_svr_config',
      header: app.globalData.headers,
      success:function(data){
        console.log(data.data.data.fixed_mavin)
        if(data.data.result===1){
          try {
            wx.setStorageSync("config", JSON.stringify(data.data.data.fixed_mavin))
          } catch (e) {

          }
        }else{
          wx.showModal({
            title: '来自伴行的提示',
            content: data.data.message,
            showCancel:false
          })
        }
      }
    })
  },
  toTrval:function(){//去往旅游专家
    var that=this;
    var status = parseInt(this.data.status);
    console.log(status);
    if (status==1){//不可切换专家
      wx.showToast({
        title: '暂时不可更改专家',
      },2000)
    }else if(status==0){
      wx.switchTab({
        url: '../myexpert/myexpert',
      })
    }
  },
//退出登录以及退出登录处理
  out(){
  app.globalData.islogin = false;
  app.globalData.headers = {//统一头部信息
    "Content-Type": "application/x-www-form-urlencoded",
    "Device-Id": "",
    "Os": "",
    "Screen": "",
    "User-Token": ""
  }
   this.setData({
     islogin:app.globalData.islogin
   })

  },
  //页面跳转
  detail:function(e){
    if(e.target.dataset.url.indexOf("myexpert")!=-1){
     wx.switchTab({
        url: e.target.dataset.url, 
      })
    }
    wx.navigateTo({
      url:e.target.dataset.url,
    })
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    
    
  },
  onReady:function(){
    // 页面渲染完成
    
  },
  
  onShow:function(){
    // wx.navigateBack({
    app = getApp(); 
    url = app.globalData.url;
    headers = app.globalData.headers;
    // })
    console.log(headers);
    console.log(this.data);
    try{
      var status = JSON.parse(wx.getStorageSync("config"));
      var myTrval = JSON.parse(wx.getStorageSync("myTrval"));
      console.log(status)
      this.setData({
        status: status,
        myTrval: myTrval
        
      });
     
    }catch(e){

    };
    // 页面显示
    var that=this
    this.setData({
      expert:app.globalData.expert,
      islogin: app.globalData.islogin// app.globalData.islogin
      
    })
    if(this.data.islogin){//登录过后展示页面
      try {//获取页面缓存的 token
        var infor = JSON.parse(wx.getStorageSync('infor'));
        console.log(infor);
        that.setData({
          infor: infor
        })
      } catch (e) {
      };
    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})